import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_demo1/tools/keepAliveWrapper.dart';

void main() {
  runApp(const MaterialApp(
    debugShowCheckedModeBanner: false,
    home: PageViewPage(),
  ));
}

class PageViewPage extends StatefulWidget {
  const PageViewPage({super.key});

  @override
  State<PageViewPage> createState() => _PageViewPageState();
}

List listData = [
  {
    "imageUrl": "https://www.itying.com/images/flutter/1.png",
  },
  {
    "imageUrl": "https://www.itying.com/images/flutter/2.png",
  },
  {
    "imageUrl": "https://www.itying.com/images/flutter/3.png",
  }
];

class _PageViewPageState extends State<PageViewPage> {
  final List<Widget> _list = [];
  @override
  void initState() {
    super.initState();
    for (int i = 0; i < listData.length; i++) {
      _list.add(KeepAliveWrapper(
        child: PicturePage(url: listData[i]["imageUrl"]),
      ));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Page View Demo"),
      ),
      body: Swiper(pageList: _list),
    );
  }
}

class Swiper extends StatefulWidget {
  final double width;
  final double height;
  final List<Widget> pageList;
  const Swiper({
    super.key,
    this.width = double.infinity,
    this.height = 200,
    required this.pageList,
  });

  @override
  State<Swiper> createState() => _SwiperState();
}

class _SwiperState extends State<Swiper> {
  late PageController _pageController;
  int _currentPageIndex = 0;
  late Timer t;
  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: 0);
    const duration = Duration(seconds: 3);
    t = Timer.periodic(duration, (timer) {
      _pageController.animateToPage(
        ((_currentPageIndex + 1) % widget.pageList.length),
        curve: Curves.linear,
        duration: const Duration(milliseconds: 500),
      );
    });
  }

  @override
  void dispose() {
    super.dispose();
    t.cancel();
    _pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        SizedBox(
          width: double.infinity,
          height: 200,
          child: PageView.builder(
            controller: _pageController,
            itemBuilder: (context, index) {
              return widget.pageList[_currentPageIndex];
            },
            onPageChanged: (index) {
              print("index:$index");
              setState(() {
                _currentPageIndex = index % widget.pageList.length;
              });
            },
            itemCount: 10000,
          ),
        ),
        Positioned(
          bottom: 10,
          left: 0,
          right: 0,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: List.generate(widget.pageList.length, (index) {
              return Container(
                margin: const EdgeInsets.fromLTRB(2, 0, 2, 0),
                width: 10,
                height: 10,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: _currentPageIndex == index ? Colors.blue : Colors.grey,
                ),
              );
            }).toList(),
          ),
        ),
      ],
    );
  }
}

class PicturePage extends StatefulWidget {
  final String url;
  final double width;
  final double height;
  const PicturePage({
    super.key,
    required this.url,
    this.width = double.infinity,
    this.height = 200,
  });

  @override
  State<PicturePage> createState() => _PicturePageState();
}

class _PicturePageState extends State<PicturePage> {
  @override
  Widget build(BuildContext context) {
    print(widget.url);
    return SizedBox(
      width: widget.width,
      height: widget.height,
      child: Image.network(widget.url, fit: BoxFit.cover),
    );
  }
}
